// 简洁版递归
function tree(arr, pid = 0) {
  return arr
    .filter(_ => _.pid === pid)
    .map(_ => ({ ..._, children: tree(arr, _.id) }));
}

/**
 * 不考虑性能递归做法
 * 递归查找，获取children
 */
const getChildren = (data, result, pid) => {
  for (const item of data) {
    if (item.pid === pid) {
      const newItem = { ...item, children: [] };
      result.push(newItem);
      getChildren(data, newItem.children, item.id);
    }
  }
};

/**
 * 转换方法
 */
const arrayToTree = (data, pid) => {
  const result = [];
  getChildren(data, result, pid);
  return result;
};

/** 扁平数据结构转Tree */
const arr = [
  { id: 1, name: '部门1', pid: 0 },
  { id: 2, name: '部门2', pid: 1 },
  { id: 3, name: '部门3', pid: 1 },
  { id: 4, name: '部门4', pid: 3 },
  { id: 5, name: '部门5', pid: 4 }
];

console.log(arrayToTree(arr));
